package com.shaoyu.tools.testcase.controller;

import com.alibaba.fastjson.JSON;

import com.shaoyu.spring.study.validate.annotation.ValidateInterceptMethod;
import com.shaoyu.spring.study.validate.core.ValidateContext;
import com.shaoyu.tools.testcase.util.SpringUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @Desc :
 * @Author: shaoyu
 * @Date : 2022/3/1 11:25
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/{beanName}/{methodName}/{params}")
    public Object testBean(@PathVariable("beanName") String beanName, @PathVariable("methodName")String methodName, @PathVariable("params") String params) throws InvocationTargetException, IllegalAccessException {
        Object bean = SpringUtil.getApplicationContext().getBean(beanName);
        Class<?> aClass = bean.getClass();
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            String name = method.getName();
            if(name.equals(methodName)){
                Class<?>[] parameterTypes = method.getParameterTypes();
                if(parameterTypes == null || parameterTypes.length == 0){
                    return method.invoke(bean);
                }
                String[] split = params.split("[|]");
                if(split.length!=parameterTypes.length){
                    return "参数个数不对";
                }
                Object[] param = new Object[split.length];
                for (int i = 0; i < parameterTypes.length; i++) {
                    Class<?> parameterType = parameterTypes[i];
                    String s = split[i];
                    param[i] = JSON.parseObject(s, parameterType);
                }
                return method.invoke(bean,param);
            }
        }
        System.out.println("调用Bean["+beanName+"]的方法:["+ methodName+"]完毕!");
        return "找不到Bean["+beanName+"]的方法:"+ methodName;
    }


    @RequestMapping("/validate")
    @ValidateInterceptMethod("validateP1")
    public Long validate(@PathParam("p1") Long p1, @PathParam("p2") Long p2){
        return p1 + p2;
    }


    @RequestMapping("/validate1")
    @ValidateInterceptMethod
    public Long testValidate(@PathParam("p1") Long p1, @PathParam("p2") Long p2){
        Object checkResult = ValidateContext.getCurrentContext().get("checkResult");
        return Objects.isNull(checkResult)?(p1 + p2 ): 1000;
    }


    @RequestMapping("/validate2")
    @ValidateInterceptMethod(value={"validateP1","validateP2"},alias = {"p1:p3"})
    public String testValidate2(@PathParam("p1") String p1, @PathParam("p2") String p2){
        return p1 + p2 ;
    }


    @RequestMapping("/validate3")
    @ValidateInterceptMethod("validateP1")
    public String validate3(@PathParam("p1") String p1, @PathParam("p2") String p2){
        Object checkResult = ValidateContext.getCurrentContext().get("checkResult");
        return Objects.isNull(checkResult)?(p1 + p2 ): checkResult.toString();
    }

}
